<?php
class PhotoController extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'photo', '', TRUE );
	
	}
function getPhoto() {
		$id = $_POST ['id_Obj'];
		$result = $this->photo->getPhotoPath ( $id );
		print(json_encode($result));
		
	}
	function savePhoto() {
		function scale_image($image, $target) {
			if (! empty ( $image )) //the image to be uploaded is a JPG I already checked this
{
				$source_image = imagecreatefromjpeg ( $image );
				$source_imagex = imagesx ( $source_image );
				$source_imagey = imagesy ( $source_image );
				
				$dest_imagex = 300;
				$dest_imagey = 200;
				
				$image2 = imagecreatetruecolor ( $dest_imagex, $dest_imagey );
				imagecopyresampled ( $image2, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey );
				
				imagejpeg ( $image2, $target, 100 );
			
			}
		}
		$target_path = "./image/";
		$target_path = $target_path . basename ( $_FILES ['uploadedfile'] ['name'] );
		if (move_uploaded_file ( $_FILES ['uploadedfile'] ['tmp_name'], $target_path )) {
			scale_image($target_path,$target_path);
			//$host = "http://192.168.233.1/CI/image/";
			
			echo (basename ( $_FILES ['uploadedfile'] ['name'] ));
		} else {
			echo 0;
		}
	}
	function addPhoto() {
		$id_User = $_POST ['id_User'];
		$id_Obj = $_POST ['id_Obj'];
		$path = $_POST ['path'];
		$id_Photo = $this->photo->addPhoto ( $id_User, $id_Obj, $path );
		print $id_Photo;
	}
}